package com.project.servlet;

import com.project.bean.ManBean;
import com.project.service.IManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;

@Controller
public class AddServlet extends HttpServlet {
    @Autowired
    private IManService service;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //将客户端的表单数据封装成实体对象
        ManBean man = new ManBean();
        man.setName(req.getParameter("name"));
        man.setBirthday(LocalDate.parse(req.getParameter("birthday")));
        //调用业务方法完成添加
        service.add(man);
        //重定向到findAllServlet显示数据
        resp.sendRedirect("/findAll");


    }
}
